#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QListWidget>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    auto listWidget = new QListWidget(this);
    QListWidgetItem * listWidgetItem = new QListWidgetItem;
    listWidgetItem->setText("listItem");
    QPixmap pixmap(50,50);
    pixmap.fill(Qt::blue);
    listWidgetItem->setIcon(pixmap);
    listWidgetItem->setToolTip("this is list item");
    listWidget->insertItem(1,listWidgetItem);
    QListWidgetItem * listWidgetItem2 = new QListWidgetItem;
    listWidgetItem2->setText("listItem2");
    QPixmap pixmap2(50,50);
    pixmap2.fill(Qt::green);
    listWidgetItem2->setIcon(pixmap2);
    listWidgetItem2->setToolTip("this is list item2");
    listWidget->insertItem(2,listWidgetItem2);
    listWidget->sortItems(Qt::DescendingOrder);
    listWidget->show();
    this->setCentralWidget(listWidget);

    _treeWidget = new QTreeWidget();
    //treewidget 要设置列数
    _treeWidget->setColumnCount(2);
    QStringList headers;
    headers << "name" << "year";
    _treeWidget->setHeaderLabels(headers);
    //添加项目
    //父节点是_treeWidget
    QTreeWidgetItem * grade1 = new QTreeWidgetItem(_treeWidget);
    grade1->setText(0,"Grade1");
    grade1->setText(1,"???");
    //父节点是grade1
    QTreeWidgetItem * student = new QTreeWidgetItem(grade1);
    student->setText(0,"Tom");
    student->setText(1,"1996");
    //父节点是grade1, 在student之后
    QTreeWidgetItem * student2 = new QTreeWidgetItem(grade1, student);
    student2->setText(0,"Zack");
    student2->setText(1,"1988");
    //父节点是_treeWidget, 在grade1之后
    QTreeWidgetItem * grade2 = new QTreeWidgetItem(_treeWidget,grade1);
    grade2->setText(0,"Grade2");
    QTreeWidgetItem * student3 = new QTreeWidgetItem(grade1, student2);
    student3->setText(0,"Will");
    student3->setText(1,"1989");
    _treeWidget->show();


    // auto *parent_student3 = student3->parent();
    // //获取student3在其父节点下的index
    // auto index_student3 = parent_student3->indexOfChild(student3);
    // //根据index删除student3
    // delete parent_student3->takeChild(index_student3);
    // //删除grade2
    auto * parent_grade1 = grade1->parent();
    //grade2为顶层节点，所以其父节点必为null
    assert(parent_grade1 == nullptr);
    //通过treewidget获取grade2的index
    auto index_grade1 = _treeWidget->indexOfTopLevelItem(grade1);
    delete _treeWidget->takeTopLevelItem(index_grade1);
}

MainWindow::~MainWindow()
{
    delete ui;
}
